Skip to main content

Math Functions

max

max(X, Y)

Return the greater value of X,Y.

min

min(x,y)

Return the smaller value of X,Y.

mid

mid(x,y,z)

Return the middle value after X, Y, and Z sizes are arranged.

ceil

ceil(x)

Return the integer closest to x and >=x, which is rounded up.

flr

flr(x)

Return the integer closest to x and <=x, which is rounded down.

sin/cos

sin(x),cos(x)

Return the sine/cosine function value of x. Note that unlike math.sin/math.cos, here y is reversed according to the screen coordinate direction.

x is the angle, corresponding to the counterclockwise direction on the screen, and the whole circle corresponds to 0..1.

For example, the far right (0 degrees) is 0, and the far left (180 degrees) is 0.5.

atan2

atan2(dx,dy)

According to dx, dy converted to an angle (0..1).

The angle corresponds to the counterclockwise direction of the screen.

atan2(0, -1) -- 0.25

sqrt

sqrt(x)

Calculate the square root of x, if x<0, return 0.

abs

abs(x)

Return the absolute value of x.

sgn

sgn(x)

Return the sign of x, if x<0 return -1, if x>=0 return 1.

band/bor/bxor/bnot/shl/shr/lshr/rotl/rotr

bit运算

band(X, Y) -- x&Y
bor(X, Y) -- x|y
bxor(X, Y) -- x^Y
bnot(X) -- ~X
shl(X, N) -- shift left, fill right with 0
shr(X, N) -- Arithmetic shift right, fill sign bit on the left
lshr(X, N) -- logical shift right, fill left with 0
rotl(X, N) -- rotate left
rotr(X, N) -- rotate right

There is a difference between the bit operation function and the bit operation symbol. The function will not report an exception when the parameter is illegal, but the symbol operation will directly throw an exception.

rnd

rnd(X)

Return a random number n, 0 <= n < x.

Note that the return value is not an integer, if you need an integer, use flr(rnd(x)).

If x is an array, then randomly returns an element value from table[1] to table[#table].

srand

srand(X)

Set the pseudo-random number seed. The same seed will result in the same random sequence.